# -*- coding: UTF-8 -*-
"""
描述：本段代码定时从89免费代理API接口获取可用的代理IP，存入IP池中
"""
import threading
import time
import requests
from scrapy.http import response, HtmlResponse
from .import dataip5


# 获取代理IP的线程类
class GetIpThread(threading.Thread):
    def __init__(self, apiUrl, fetchSecond):
        super(GetIpThread, self).__init__()
        self.fetchSecond = fetchSecond
        self.apiUrl = apiUrl

    def run(self):
        while True:
            # 获取IP列表
            text_url = 'http://www.krisdika.go.th/wps/portal/general_en'
            lurl = requests.get(self.apiUrl).content.decode()
            res = HtmlResponse(url=self.apiUrl, encoding='utf8', body=lurl)
            try:
                trs = res.xpath('//body/text()')
                for tr in trs:
                    proxy = tr.get().strip()
                    proxies = {
                        'http': 'http://' + proxy,
                        'https': 'https://' + proxy,
                    }
                    try:
                        text_response = requests.get(text_url, proxies=proxies, verify=False)
                        print('该ip可用:' + proxy)
                        dataip5.IPPOOL = proxy
                    except Exception as e:
                        print('该ip不适用' + proxy + str(e))
            except Exception as e:
                print('操作异常：' + str(e))
            # 休眠
            time.sleep(self.fetchSecond)

'''
# 89免费代理ip提取线程
    # 获取IP的API接口
    apiUrl = "http://www.89ip.cn/tqdl.html?api=1&num=60&port=&address=泰国&isp="
    # 获取IP时间间隔，建议为300秒
    fetchSecond = 300
    # 开始自动获取IP
    GetIpThread(apiUrl, fetchSecond).start()
'''

